/**
 * 
 */
package steffens.java.gui.defaultui.bar;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextField;

import steffens.java.gui.defaultui.general.DefaultBar;

/**
 * @author Steffen Baumann
 * @version 1.0 
 *
 */
public class FieldStatusBar extends DefaultBar{

	List<JTextField> fields = new ArrayList<JTextField>();
	private List<Integer> fieldWidths = new ArrayList<Integer>();
	private int fieldsHeight = 16;
	private static final int DEFAULT_WIDTH = 60;
	private static final int DEFAULT_INSET_X = 1;
	private static final int DEFAULT_INSET_Y = 0;
	private Dimension insets = new Dimension(DEFAULT_INSET_X, DEFAULT_INSET_Y);
	private List<Color> textColors = new ArrayList<Color>();
	public static final Color DEFAULT_TEXT_COLOR = Color.GRAY;
	
	public FieldStatusBar() {
		this(0);
	}
	/**
	 * 
	 */
	public FieldStatusBar(int fieldCount) {
		initFields(fieldCount);
		setLayout();
		updateBar();
	}

	private void initFields(int fieldCount) {
		for(int i = 0; i < fieldCount; i++){
			addField(i);
		}
	}

	private void setLayout(){
		getBarContainer().setLayout(new GridBagLayout());
	}
	
	public void addField(int index){
		JTextField field = new JTextField();
		setFieldProperties(field);
		fields.add(index, field);
		fieldWidths.add(index, DEFAULT_WIDTH);
		textColors.add(index, DEFAULT_TEXT_COLOR);
		updateBar();
	}
	
	public void removeField(int index){
		fields.remove(index);
		updateBar();
	}
	
	public void setTextToField(String text, int index){
		fields.get(index).setText(text);
	}
	
	@Override
	public void updateBar() {
		Container con = getBarContainer();
		Dimension minimalSize = getMinimumDimension();
		con.setMinimumSize(minimalSize);
		con.setPreferredSize(minimalSize);
		validateBar(con);
		con.setBackground(getBackground());
		con.validate();
		Container parent = con.getParent();
		if(parent != null){
			parent.validate();
		}
		if(getParentNotifier() != null) getParentNotifier().notifyParentAboutCahnge();
	}
	private Dimension getFieldContainerDimension() {
		int fieldsWidth = getFieldsWidthSum();
		int fieldsHeight = this.fieldsHeight;
		int insetsHeight = insets.height;
		int insetsWidth = insets.width;
		return new Dimension((fieldsWidth+insetsWidth)+insetsWidth,insetsHeight*2+fieldsHeight);
	}
	
	private int getFieldsWidthSum(){
		int fieldsWidth = 0;
		for(int width: fieldWidths){
			fieldsWidth += width;
		}
		return fieldsWidth;
	}
	
	public Dimension getMinimumDimension(){
		return getFieldContainerDimension();
	}

	private void validateBar(Container con){
		con.removeAll();
		for(int i = 0; i  < fields.size(); i++){
			JTextField field = fields.get(i);
			field.setSize(getSizeOfField(i));
			field.setPreferredSize(getSizeOfField(i));
			field.setMinimumSize(getSizeOfField(i));
			field.setMaximumSize(getSizeOfField(i));
			getBarContainer().add(field, getConstraints(i));
		}
	}
	private GridBagConstraints getConstraints(int index){
		GridBagConstraints placement = new GridBagConstraints();
		placement.anchor = GridBagConstraints.LINE_START;
		placement.gridx = index;
		placement.gridy = 0;
		placement.weightx = 0.5;
		placement.insets = new Insets(insets.height, insets.width, insets.height, insets.width);
		placement.fill = GridBagConstraints.HORIZONTAL;
		return placement;
	}
	
	private void setFieldProperties(JTextField field){
		field.setEditable(false);
		field.setForeground(Color.gray);
	}
	
	public void setSizeOfAllFields(Dimension fieldSizes){
		for(int i = 0; i < fieldWidths.size(); i++){
			setWidthToField(i, fieldSizes.width);
		}
		this.fieldsHeight = fieldSizes.height;
		updateBar();
		
	}
	public List<Dimension> getSizeOfAllFields(){
		List<Dimension> sizes = new ArrayList<>();
		for(int width: fieldWidths){
			sizes.add(new Dimension(width, fieldsHeight));
		}
		return sizes;
	}
	
	public int getHeigthOfAllFields(){
		return fieldsHeight;
	}
	
	public void setHeigthOfAllFields(int fieldsHeight){
		this.fieldsHeight = fieldsHeight;
		updateBar();
	}
	
	public Dimension getSizeOfField(int index){
		if(fieldWidths.isEmpty()) return new Dimension(0,0);
		return new Dimension(fieldWidths.get(index),fieldsHeight);
	}
	
	private void setWidthToField(int index, int width){
		fieldWidths.set(index, width);
	}
	
	public void setWidthOfField(int index, int width){
		setWidthToField(index, width);
		updateBar();
	}
	public List<Color> getTextColorOfAllFields(){
		return textColors;
	}
	public Color getTextColorOfField(int index){
		return textColors.get(index);
	}
	public void setTextColorOfField(int index, Color color){
		fields.get(index).setForeground(color);
		textColors.set(index, color);
	}
}
